<?php
#                                                                                                                                                             
# author: Cody Sorgenfrey                                       
# (c)2012 South Hill Calvary Chapel                                               
#                                                                                 
include_once("./root.inc.php");
include_once("${root}/mysql/database_info.php");
include_once("${root}/mysql/common.php");
include_once("${root}/common.php");

function writeToRSS(){

	$result = mysql_query("SELECT * FROM `needs` WHERE `email`='".$_GET['email']."' && `category`='".$_GET['cat']."' && `title`='".$_GET['title']."'");
	
	$post = mysql_fetch_array($result);
	
	$pubDate = date("r");
	
	$newItem = "\n\t<item>\n\t\t<title>".$post['title']."</title>\n\t\t<link>http://www.southhillcalvary.org/needsPosted.php?id=".$post['id']."</link>\n\t\t<description>".$post['summary']."</description>\n\t\t<pubDate>".$pubDate."</pubDate>\n\t</item>\n</channel>\n</rss>";
	
	$fileRef = fopen("/m/needsrss.xml", "r+");
	
	fseek($fileRef, -18, SEEK_END);
	
	fwrite($fileRef, $newItem);
	
	fclose($fileRef);
}

function approvePost(){
	
	mysql_query("UPDATE `needs` SET `pending`='0' WHERE `email`='".$_GET['email']."' && `category`='".$_GET['cat']."' && `title`='".$_GET['title']."'");
	
	writeToRSS();
}

function denyPost(){
	
	mysql_query("DELETE FROM `needs` WHERE `email`='".$_GET['email']."' && `category`='".$_GET['cat']."' && `title`='".$_GET['title']."'");
}

function emailConfirmation(){
	
	$message = "Your post entitled \"".$_GET['title']."\" on the SHCC Needs Board has been approved and posted, It will be taken down after a week. If you didn't receive any help, feel free to post it again.";
	
	return mail($_GET['email'], "Your Post on the SHCC Needs Board", $message, "From: webservant@southhillcalvary.org");
}

function emailDenial(){
	
	$message = "Your post entitled \"".$_GET['title']."\" on the SHCC Needs Board has not been approved or posted, Feel free to reply, or contact the church office for further information.";
	
	return mail($_GET['email'], "Your Post on the SHCC Needs Board", $message, "From: webservant@southhillcalvary.org");
}

$title = "Review Need";
$meta_key='';
$meta_desc="";
print_html_head($title ,$meta_key,$meta_desc);
print_java_head(null);

connect_select_test("needs");

begin_body();
page_title($title);

if (!($_GET['cat'] && $_GET['email'] && $_GET['approve']) ){
	
	printf("Sorry this page is for SHCC staff only.");
}
else{
	
	if ($_REQUEST['needsLogin'] == 1){
		
		if ($_GET['approve'] == "TRUE"){
			
			approvePost();
			
			if(emailConfirmation()){
			
				printf("Email sent, and post has been posted.");
			}
			else {
				
				printf("Unable to send email, try again.");
			}	
		}
		else if ($_GET['approve'] == "FALSE"){
			
			denyPost();
			
			if(emailDenial()){
			
				printf("Email sent, and post has been deleted.");
			}
			else {
				
				printf("Unable to send email, try again.");
			}
		}
	}
	else {
	
		$vars = array("cat", $_GET['cat'], "email", $_GET['email'], "approve", $_GET['approve']);
		
		printNeedsLogin("/needsReview.php?cat=".$_GET['cat']."&email=".$_GET['email']."&approve=".$_GET['approve'], $vars);
	}
}

end_page_title();
end_body();

?>